package com.sanjiahantang.youjiarole.module.common.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.http.BaseDelegate;
import com.sanjiahantang.youjiarole.http.OkHttpClientManager;
import com.sanjiahantang.youjiarole.module.city.beans.CityBean;
import com.sanjiahantang.youjiarole.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiarole.module.me.scheme.beans.MeCaseOBean;
import com.sanjiahantang.youjiarole.module.money.beans.BankCardBean;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zy on 2018/3/23.
 */

public class CommonModelUtils implements StaticValue{

    /**
     * 获取支持银行卡列表
     * @param context
     * @param key
     * @param listener
     */
    public static final String TYPE_BANK = "ghtbankcode";
    public static void findDicInfoByParentIdForCommon(final Context context, String key, final BaseView.OnLoadListener listener){
        Map<String,String> param = new HashMap<>();
        param.put("parentId",key);
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/system/dic/findDicByParentForPhone",
                param, new BaseDelegate.ResultCallback<ConditionBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        Utils.showToast(context,e.getMessage());
                        listener.onLoad(null);
                    }

                    @Override
                    public void onResponse(ConditionBean response, Object tag) {
                        if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                            listener.onLoad(response.getData());
                        }else if (response != null){
                            listener.onLoad(null);
                            Utils.showToast(context,response.getMessage());
                        }else{
                            listener.onLoad(null);
                            Utils.showToast(context,"网络错误");
                        }
                    }
                });
    }

    /**
     * 获取城市三级列表
     * @param context
     * @param callBack
     */
    public static void getAllArea(final Context context, final BaseView.OnLoadListener callBack) {
        try {
            InputStream is = context.getResources().getAssets().open("province_data.json");
            InputStreamReader isr = new InputStreamReader(is,"UTF-8");
            Gson gson = new Gson();
            List<CityBean.Province> provinces = gson.fromJson(isr,new TypeToken<List<CityBean.Province>>(){}.getType());
            ArrayList<String> provinceList = new ArrayList<String>();
            ArrayList<ArrayList<String>> cityList = new ArrayList<ArrayList<String>>();
            ArrayList<ArrayList<ArrayList<String>>> areaList = new ArrayList<ArrayList<ArrayList<String>>>();
            if (provinces != null){
                for (int i = 0,max = provinces.size();i<max;i++){
                    provinceList.add(provinces.get(i).getName());
                    if (null != provinces.get(i).getCity()){
                        ArrayList<String> cities = new ArrayList<String>();
                        ArrayList<ArrayList<String>> areas = new ArrayList<ArrayList<String>>();
                        for (int j = 0,max_j = provinces.get(i).getCity().size();j<max_j;j++){
                            cities.add(provinces.get(i).getCity().get(j).getName());
                            areas.add((ArrayList<String>) provinces.get(i).getCity().get(j).getArea());
                        }
                        cityList.add(cities);
                        areaList.add(areas);
                    }
                }
            }
            Object[] result = new Object[3];
            result[0] = provinceList;
            result[1] = cityList;
            result[2] = areaList;
            callBack.onLoad(result);
        } catch (IOException e) {
            // Should never happen!
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取默认银行卡
     * @param context
     * @param listener
     */
    public static void searchBankCardDefault(final Context context, final BaseView.OnLoadListener listener){
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/money/bank/role/searchBankCardDefault",
                param, new BaseDelegate.ResultCallback<BankCardBean.Card>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        listener.onLoad(null);
                    }

                    @Override
                    public void onResponse(BankCardBean.Card response, Object tag) {
                        if (response == null || response.getData() == null || TextUtils.isEmpty(response.getData().getMbciId())){
                            listener.onLoad(null);
                        }else{
                            listener.onLoad(response.getData());
                        }
                    }
                });
    }

    /**
     * 根据配置key查询value
     * @param context
     * @param key
     * @param listener
     */
    public static final String CONFIG_CODE_AGREE = "agreement";
    public static void findConfigCode(Context context, String key, final BaseView.OnLoadListener listener){
        Map<String,String> param = new HashMap<>();
        param.put("configCode",key);
        OkHttpClientManager.postAsyn(context,ConfigValue.API_URL + "/system/config/findConfigCode",
                param, new BaseDelegate.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        listener.onLoad(null);
                    }

                    @Override
                    public void onResponse(String response, Object tag) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (jsonObject.has("configValue")){
                                listener.onLoad(jsonObject.get("configValue"));
                            }else{
                                listener.onLoad(null);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            listener.onLoad(null);
                        }
                    }
                });
    }
}
